package com.xtravel.tweet.database.dao;

import android.content.Context;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.table.TableUtils;
import com.xtravel.common.database.helper.DatabaseHelper;
import com.xtravel.tweet.database.dto.Tweet;
import com.xtravel.tweet.database.dto.TweetSource;
import com.xtravel.tweet.database.po.TweetPo;
import com.xtravel.tweet.database.po.TweetSourcePo;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/10/9.
 */
public class TweetDao {

    private Dao<TweetPo, Integer> tweetOpe;
    private Dao<TweetSourcePo, Integer> sourceOpe;

    private DatabaseHelper helper;

    public TweetDao(Context context) {
        try {
            helper = DatabaseHelper.getHelper(context);
            tweetOpe = helper.getDao(TweetPo.class);
            sourceOpe = helper.getDao(TweetSourcePo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void saveAll(List<Tweet> tweetList) throws SQLException {
        List<TweetPo> tweetPoList = new ArrayList<TweetPo>();
        List<TweetSourcePo> tweetSourcePoList = new ArrayList<TweetSourcePo>();

        for (Tweet tweet : tweetList) {
            TweetPo tweetpo = new TweetPo();
            tweetpo.setId(tweet.id);
            tweetpo.setObjId(tweet.objId);
            tweetpo.setTweetId(tweet.tweetId);
            tweetpo.setContent(tweet.content);
            tweetpo.setTweetUrl(tweet.tweetUrl);
            tweetpo.setTitle(tweet.title);
            tweetpo.setDtCreatedTime(tweet.dtCreatedTime);
            tweetpo.setRetweetCount(tweet.retweetCount);
            tweetpo.setCommentCount(tweet.commentCount);
            tweetpo.setLikeCount(tweet.likeCount);
            tweetpo.setIsPerfect(tweet.isPerfect);
            tweetpo.setCreatedTime(0L);
            tweetpo.setOnlineTime(tweet.getOnlineTime());

            tweetPoList.add(tweetpo);

            for (TweetSource ts : tweet.tweetSources) {
                tweetSourcePoList.add(getTweetSourcePo(ts, 1));
            }

            /*
            for (TweetSource ts : tweet.getImageSources()) {
                tweetSourcePoList.add(getTweetSourcePo(ts, 2));
            }
            */

            for (TweetSource ts : tweet.getLinkSources()) {
                tweetSourcePoList.add(getTweetSourcePo(ts, 3));
            }
        }

        TableUtils.clearTable(tweetOpe.getConnectionSource(), TweetPo.class);
        TableUtils.clearTable(sourceOpe.getConnectionSource(), TweetSourcePo.class);

        for (TweetPo po : tweetPoList) {
            tweetOpe.create(po);
        }
        for (TweetSourcePo po : tweetSourcePoList) {
            try {
                sourceOpe.create(po);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public List<Tweet> getAll() throws SQLException {
        List<TweetPo> tweetPoList = tweetOpe.queryForAll();
        List<TweetSourcePo> tweetSourcePoList = sourceOpe.queryForAll();

        List<Tweet> tweetList = new ArrayList<Tweet>();

        for (TweetPo tweetPo : tweetPoList) {
            Tweet tweet = new Tweet(
                    tweetPo.getId(),
                    tweetPo.getTweetId(),
                    tweetPo.getContent(),
                    tweetPo.getTweetUrl(),
                    tweetPo.getTitle(),
                    tweetPo.getDtCreatedTime(),
                    tweetPo.getRetweetCount(),
                    tweetPo.getCommentCount(),
                    tweetPo.getLikeCount(),
                    tweetPo.isPerfect(),
                    new ArrayList<TweetSource>(),
                    ""
            );
            tweet.setOnlineTime(tweetPo.getOnlineTime());

            for (TweetSourcePo spo : tweetSourcePoList) {
                TweetSource ts = getTweetSource(spo);
                if (ts.tweetId.equals(tweet.tweetId)) {
                    if (spo.getSourceType() == 1) {
                        tweet.tweetSources.add(ts);
                    }
                    if (spo.getSourceType() == 2) {
                        tweet.getImageSources().add(ts);
                    }
                    if (spo.getSourceType() == 3) {
                        tweet.getLinkSources().add(ts);
                    }
                }
            }

            tweetList.add(tweet);
        }

        return tweetList;
    }

    private TweetSource getTweetSource(TweetSourcePo spo) {
        TweetSource ts = new TweetSource(
                spo.getId(),
                spo.getType(),
                spo.getTweetId(),
                spo.getUrl(),
                spo.getShortenUrl(),
                spo.getLargeUrl(),
                spo.getOrderBy());
        return ts;
    }

    private TweetSourcePo getTweetSourcePo(TweetSource ts, Integer sourceType) {

        TweetSourcePo tsPo = new TweetSourcePo();
        tsPo.setSourceType(sourceType);

        tsPo.setId(ts.id);
        tsPo.setObjId(ts.objId);
        tsPo.setType(ts.type);
        tsPo.setTweetId(ts.tweetId);
        tsPo.setUrl(ts.url);
        tsPo.setShortenUrl(ts.shortenUrl);
        tsPo.setLargeUrl(ts.largeUrl);
        tsPo.setOrderBy(ts.orderBy);

        return tsPo;
    }

}
